package com.million.orderservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.JwtUtils;
import com.million.commonutils.R;
import com.million.orderservice.entity.Order;
import com.million.orderservice.entity.vo.OrderInfoVo;
import com.million.orderservice.entity.vo.QueryOrder;
import com.million.orderservice.service.OrderService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/orderservice/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //根据课程id生成订单接口 返回订单号
    @PostMapping("addOrderInfo/{courseId}")
    public R addOrderInfo (@PathVariable("courseId") String courseId, HttpServletRequest request) {
        //返回订单号order_np  在cookie中的token 中取到用户信息
        String orderNo = orderService.addOrderInfo(courseId, JwtUtils.getMemberIdByJwtToken(request));
        if (orderNo == null) {
            return R.error().code(28004).message("先登录");
        }
        return R.ok().data("orderNo", orderNo);
    }

    //根据订单号 得到订单的信息
    @GetMapping("getOrderInfo/{orderNo}")
    public R getOrderInfo (@PathVariable("orderNo") String orderNo) {
        Order order = orderService.getOrderInfo(orderNo);
        return R.ok().data("order", order);
    }

    //根据课程id 和用户id 查询订单状态
    //这个方法会被edu 服务调用
    @GetMapping("selectOrderStatus/{courseId}/{memberId}")
    public boolean selectOrderStatus (@PathVariable("courseId") String courseId,
                                @PathVariable("memberId") String memberId) {
      return orderService.selectOrderStatus(courseId, memberId);
    }

    //分页条件查询订单信息
    @PostMapping("queryOrderPage/{page}/{limit}")
    public R queryOrderPage(@ApiParam(name = "page", value = "当前页码", required = true)
                                @PathVariable("page") Long page,
                            @ApiParam(name = "limit", value = "每页记录数", required = true)
                                @PathVariable("limit") Long limit,
                            @RequestBody QueryOrder queryOrder){
        Page<OrderInfoVo> orderInfoVoPage = new Page<>(page,limit);
        Page<OrderInfoVo> orderList = orderService.queryOrderPage(orderInfoVoPage,queryOrder);
        return R.ok().data("orderList",orderList);
    }

    //根据订单号删除订单信息 包括删除订单日志
    @DeleteMapping("deleteOrderInfo/{orderNo}")
    public R deleteOrderInfo(@PathVariable("orderNo") String orderNo){
        orderService.deleteOrderInfo(orderNo);
        return R.ok();
    }

}

